//
//  ViewController.m
//  mac_app_icon_modifier
//
//  Created by flynn on 2022/9/27.
//

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    NSString* strAppPath = @"/Applications/Firefox.app";
    [self modifyAppIcon:strAppPath];
}

- (void)modifyAppIcon:(NSString*)strAppPath
{
    NSBundle* bundleApp = [NSBundle bundleWithPath:strAppPath];
    NSString* iconFileName = [[bundleApp infoDictionary] objectForKey:@"CFBundleIconFile"];
    if (![iconFileName hasSuffix:@"icns"]) {
        iconFileName = [iconFileName stringByAppendingString:@".icns"];
    }
    // 判断图标文件是否存在，存在的话，生成图标PNG文件
    NSString* iconFilePath = [strAppPath stringByAppendingFormat:@"%s%@","/Contents/Resources/", iconFileName];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:iconFilePath]) {
        
        return ;
    }
    
    iconFilePath = [iconFilePath stringByReplacingOccurrencesOfString:@" " withString:@"\\ "];
    iconFilePath = [iconFilePath stringByReplacingOccurrencesOfString:@"(" withString:@"\\("];
    iconFilePath = [iconFilePath stringByReplacingOccurrencesOfString:@")" withString:@"\\)"];
    
    NSImage* imgIcon = [[NSImage alloc] initWithContentsOfFile:iconFilePath];
    if (imgIcon)
    {
        NSArray* arrImgReps = [imgIcon representations];
        if (arrImgReps)
        {
            for (NSImageRep* imgRep in arrImgReps)
            {
                [self redrawImage:imgRep];
            }
        }
    }
    
    //[self generateNewIcon:iconFileName];
}
- (void)generateNewIcon:(NSString*)strAppIcon
{
    //iconutil -c icns icons.iconset -o Icon.icns
    NSError* error = nil;
    NSTask* task = [NSTask launchedTaskWithExecutableURL:[NSURL URLWithString:@"file:///usr/bin/iconutil"] arguments:@[@"-c",@"icns",@"-o",@"/tmp/icon.icns",@"/tmp/myicon.iconset"] error:&error terminationHandler:^(NSTask * _Nonnull t) {
        NSLog(@"task:%@",t);
    }];
    if (error)
    {
        NSLog(@"%@",error);
    }
}
- (void)redrawImage:(NSImageRep*)imgRep
{
    CGFloat fwidth = imgRep.pixelsWide;
    // 缩放 image
    NSImage* imgBg = [[NSImage alloc] initWithSize: CGSizeMake(fwidth, fwidth)];
    NSImage* imgMask = [NSImage imageNamed:@"TaskBarOverlayIcon.ico"];
    [imgBg lockFocus];
    [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
    [imgRep drawInRect:NSMakeRect(0, 0, fwidth, fwidth)];
    //[imgMask drawInRect:NSMakeRect(fwidth/2, 0, fwidth/2, fwidth/2)];
    [imgBg unlockFocus];
    
    NSData *imageData = [imgBg TIFFRepresentation];
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
    imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:nil];

    CGFloat fPixel = 0;
    int iRatio = 1;
    fPixel = imgRep.pixelsWide;
    iRatio = (int)fPixel/(int)imgRep.size.width;
    
    if (![[NSFileManager defaultManager]fileExistsAtPath:@"/tmp/myicon.iconset" isDirectory:nil])
    {
        [[NSFileManager defaultManager]createDirectoryAtPath:@"/tmp/myicon.iconset" withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString* strOutImgPathName = nil;
    if (iRatio>1)strOutImgPathName = [NSString stringWithFormat:@"/tmp/myicon.iconset/icon_%dx%d@%dx.png",(int)imgRep.size.width,(int)imgRep.size.width,iRatio];
    else strOutImgPathName = [NSString stringWithFormat:@"/tmp/myicon.iconset/icon_%dx%d.png",(int)imgRep.size.width,(int)imgRep.size.width];
    [imageData writeToFile:strOutImgPathName atomically:YES];
    
    
}


@end
